﻿using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public static class GlobalData
{
    public class CgData
    {
        public Texture2D Cg;
        public bool CanbeSeem;
    }
    public class MusicData
    {
        public AudioClip Music;
        public bool CanbeListened;
    }
    public static int SaveNextScene;
    static string savePath;
    static string globalPath;
    public static Dictionary<string, CgData> CgDictionary;
    public static Dictionary<string, MusicData> MusicDictionary;
    private static readonly string cgName;

    public static bool[] LetterCanbeReads;
    static GlobalData()
    {
        savePath = Application.streamingAssetsPath + "//SaveData.sav";
        if (File.Exists(savePath))
        {
            FileStream fileStream = new FileStream(savePath, FileMode.Open);
            BinaryReader br = new BinaryReader(fileStream);
            SaveNextScene = br.ReadInt32();
            br.Close();
        }
        else
            SaveNextScene = -1;

        LetterCanbeReads = new bool[3];
        CgDictionary = new Dictionary<string, CgData>();
        MusicDictionary = new Dictionary<string, MusicData>();
        globalPath = Application.streamingAssetsPath + "//Global.sav";
        CgData cgdata;
        MusicData musicdata;
        if (File.Exists(globalPath))
        {
            FileStream fileStream = new FileStream(globalPath, FileMode.Open);
            BinaryReader br = new BinaryReader(fileStream);
            int cnt = br.ReadInt32();
            string cgName;
            Texture2D cg;
            while (cnt-- > 0)
            {
                cgName = br.ReadString();
                cg = Resources.Load<Texture2D>("Cgs/" + cgName);
                cgdata = new CgData { Cg = cg, CanbeSeem = br.ReadBoolean() };
                CgDictionary.Add(cgName, cgdata);
            }

            cnt = br.ReadInt32();
            string musicName;
            AudioClip music;
            while (cnt-- > 0)
            {
                musicName = br.ReadString();
                music = Resources.Load<AudioClip>("Musics/" + musicName);
                musicdata = new MusicData { Music = music, CanbeListened = br.ReadBoolean() };
                MusicDictionary.Add(musicName, musicdata);
            }
            LetterCanbeReads[0] = br.ReadBoolean();
            LetterCanbeReads[1] = br.ReadBoolean();
            LetterCanbeReads[2] = br.ReadBoolean();
            br.Close();
        }
        else
        {
            Texture2D[] allcg = Resources.LoadAll<Texture2D>("Cgs");
            foreach (Texture2D item in allcg)
            {
                cgdata = new CgData { Cg = item, CanbeSeem = false};
                CgDictionary.Add(item.name, cgdata);
            }

            AudioClip[] allmusic = Resources.LoadAll<AudioClip>("Musics");
            foreach (AudioClip item in allmusic)
            {
                musicdata = new MusicData { Music = item, CanbeListened = false };
                MusicDictionary.Add(item.name, musicdata);
            }
            MusicDictionary["03 - Rain Of Recollection"].CanbeListened = true;
            LetterCanbeReads[0] = false;
            LetterCanbeReads[1] = false;
            LetterCanbeReads[2] = false;
        }
    }
    public static void Save()
    {
        if (SaveNextScene == 0)
            SaveNextScene = -1;
        FileStream fileStream = new FileStream(savePath, FileMode.OpenOrCreate);
        BinaryWriter bw = new BinaryWriter(fileStream);
        bw.Write(SaveNextScene);
        bw.Close();

        fileStream = new FileStream(globalPath, FileMode.OpenOrCreate);
        bw = new BinaryWriter(fileStream);
        bw.Write(CgDictionary.Count);
        foreach (var item in CgDictionary.Values)
        {
            bw.Write(item.Cg.name);
            bw.Write(item.CanbeSeem);
        }

        bw.Write(MusicDictionary.Count);
        foreach (var item in MusicDictionary.Values)
        {
            bw.Write(item.Music.name);
            bw.Write(item.CanbeListened);
        }
        bw.Write(LetterCanbeReads[0]);
        bw.Write(LetterCanbeReads[1]);
        bw.Write(LetterCanbeReads[2]);
        bw.Close();
    }
}
